package org.lyg.controller;

import com.github.pagehelper.PageInfo;

import org.apache.shiro.SecurityUtils;
import org.lyg.domain.Blog;
import org.lyg.domain.BlogCondition;
import org.lyg.domain.User;
import org.lyg.domain.UserCondition;
import org.lyg.service.IBlogService;
import org.lyg.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class AdminController {
    private static final String USER_LIST_PAGE = "user_list_page";
    private static final String BLOG_LIST_PAGE = "blog_list_page";
    private static final int USER_LIST_PAGE_SIZE = 10;
    private static final int BLOG_LIST_PAGE_SIZE = 10;
    @Autowired
    IUserService userService;
    @Autowired
    IBlogService blogService;

    @RequestMapping
    public String admin(ModelMap modelMap) {
        addUserSelf(modelMap);
        return "admin/admin";
    }

    @RequestMapping("/user/list/{page}")
    public String userList(@PathVariable("page") int page, ModelMap modelMap, HttpServletRequest req) {
        modelMap.addAttribute("userManager", true);
        System.out.println(page);
        String kw=req.getParameter("kw");
        PageInfo<User> userPageInfo = userService.list(page,USER_LIST_PAGE_SIZE,new UserCondition(kw));
        System.out.println(userPageInfo.getList());
        modelMap.addAttribute("userPageInfo", userPageInfo);
        modelMap.addAttribute("kw",kw);
        req.getSession().setAttribute(USER_LIST_PAGE, page);
        addUserSelf(modelMap);
        return "admin/admin";
    }

    @RequestMapping("/user/enable/{id}")
    public String enableUser(@PathVariable("id") int id, HttpServletRequest req) {
        return enableOrDisableUser(id, req,false);
    }

    @RequestMapping("/user/disable/{id}")
    public String disableUser(@PathVariable("id") int id, HttpServletRequest req) {
        return enableOrDisableUser(id, req,true);
    }

    private String enableOrDisableUser(int id, HttpServletRequest req,boolean isDisable) {
        int page = (int) req.getSession().getAttribute(USER_LIST_PAGE);
        User user = new User(id);
        user.setDisable(isDisable);
        userService.update(user);
        return "redirect:/admin/user/list/" + page;
    }

    @RequestMapping("/user/lock/{id}")
    public String lockUser(@PathVariable("id") int id, HttpServletRequest req) {
        return lockOrUnlockUser(id, req,true);
    }

    @RequestMapping("/user/unlock/{id}")
    public String unlockUser(@PathVariable("id") int id, HttpServletRequest req) {
        return lockOrUnlockUser(id, req,false);
    }

    private String lockOrUnlockUser(int id, HttpServletRequest req,boolean isLock) {
        int page = 0;
        try {
            page = (int) req.getSession().getAttribute(USER_LIST_PAGE);
        } catch (Exception e) {
        }
        User user = new User(id);
        user.setLocked(isLock);
        userService.update(user);
        return "redirect:/admin/user/list/" + page;
    }

    @RequestMapping("/blog/list/{page}")
    public String blogList(@PathVariable("page") int page, ModelMap modelMap, HttpServletRequest req) {
        req.getSession().setAttribute(BLOG_LIST_PAGE, page);
        String kw = req.getParameter("kw");
        Integer uid = req.getParameter("uid")==null?null: Integer.valueOf(req.getParameter("uid"));
        modelMap.addAttribute("blogManager", true);
        PageInfo<Blog> blogPageInfo = blogService.list(BLOG_LIST_PAGE_SIZE, page,new BlogCondition(kw,null,uid,null));
        modelMap.addAttribute("kw",kw);
        modelMap.addAttribute("uid",uid);
        modelMap.addAttribute("blogPageInfo", blogPageInfo);
        addUserSelf(modelMap);
        return "admin/admin";
    }

    @RequestMapping("/blog/del/{id}")
    public String delBlog(@PathVariable("id") int id, HttpServletRequest req) {
        return delOrUndelBlog(id, req);
    }

    @RequestMapping("/blog/undel/{id}")
    public String undelBlog(@PathVariable("id") int id, HttpServletRequest req) {
        return delOrUndelBlog(id, req);
    }

    private String delOrUndelBlog(int id, HttpServletRequest req) {
        int page = 0;
        try {
            page = (int) req.getSession().getAttribute(USER_LIST_PAGE);
        } catch (Exception e) {
        }
        Blog blog = new Blog(id);
        blog.setDel(false);
        blogService.update(blog);
        return "redirect:/admin/blog/list/" + page;
    }

    private void clearAuthorizationInfo(String username){
    }

    private void addUserSelf(ModelMap modelMap) {
        modelMap.addAttribute("userSelf", SecurityUtils.getSubject().getPrincipal());
    }
}
